//
//  newController.m
//  newTestGit
//
//  Created by Hongshi on 15/10/29.
//  Copyright © 2015年 Hongshi. All rights reserved.
//

#import "newController.h"
#import "ALView+PureLayout.h"

@interface newController () <UIActionSheetDelegate,iCarouselDataSource,iCarouselDelegate>
{
    UIButton *button;
    UIButton *button1;
}
@property (copy , nonatomic) NSArray *array;
@property (nonatomic,copy) NSArray *imagesArray;
@property (nonatomic, assign) BOOL wrap;
@property (nonatomic , strong) NSMutableArray *items;
@property (nonatomic, assign) BOOL useButtons;

@end

@implementation newController

@synthesize carousel;
@synthesize wrap;
@synthesize items;
@synthesize useButtons;

//- (void)toggleButtons:(UIButton *)btn
//{
//    useButtons = !useButtons;
//    [btn setTitle:useButtons? @"Disable buttons": @"Enable buttons" forState:UIControlStateNormal];
//    [carousel reloadData];
//}
- (void) setUp
{
    self.wrap = YES;
    self.items = [NSMutableArray array];
    for (int i = 0;i < 14;i++)
    {
        [self.items addObject:@(i)];
    }
}
- (id) initWithnibName:(NSString *) nibNameOrNil bundle:(NSBundle *) nibBundleOrNil
{
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]))
    {
        [self setUp];
    }
    return self;
}

- (instancetype) initWithCoder:(NSCoder *)aDecoder
{
    if ((self = [super initWithCoder:aDecoder])) {
        
        [self setUp];
    }
    return self;
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithWhite:0.298 alpha:1.000];
    
    [self createBackButtonWithTitle:@"返回"];
    
    //self.view.backgroundColor = [UIColor whiteColor];
    self.array = @[@"Linear", @"Rotary", @"Inverted Rotary", @"Cylinder", @"Inverted Cylinder", @"Wheel", @"Inverted Wheel", @"CoverFlow", @"CoverFlow2", @"Time Machine", @"Inverted Time Machine", @"Custom"];
    
    self.imagesArray = @[@"1.png",@"2.png",@"3.png",@"4.png",@"5.png",@"6.png",@"7.png",@"8.png",@"9.png",@"10.png",@"11.png",@"12.png",@"13.png",@"14.png"];
    
    button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectZero;
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 10.0f;
    [button setTitle:@"改变风格" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.backgroundColor = [UIColor colorWithRed:0.000 green:0.502 blue:1.000 alpha:1.000];
    [button addTarget:self action:@selector(changeStyle) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    
    button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button1.frame = CGRectZero;
    button1.layer.masksToBounds = YES;
    button1.layer.cornerRadius = 10.0f;
    [button1 setTitle:@"Disable buttons" forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button1.backgroundColor = [UIColor colorWithRed:0.000 green:0.502 blue:1.000 alpha:1.000];
    [button1 addTarget:self action:@selector(pressAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
    
    carousel = [[iCarousel alloc] init];
    carousel.dataSource = self;
    carousel.delegate = self;
    carousel.type = iCarouselTypeCoverFlow2;
    [self.view addSubview:carousel];
    
    [self.view setNeedsUpdateConstraints];
}



- (void) updateViewConstraints
{
    [super updateViewConstraints];
    
    
    [button autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:50];
    [button autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:50];
    [button autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:84];
    [button autoSetDimension:ALDimensionHeight toSize:44];
    
    [button1 autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:20];
    [button1 autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:50];
    [button1 autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:50];
    [button1 autoSetDimension:ALDimensionHeight toSize:44];
    
    [self.carousel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:button withOffset:20];
    [self.carousel autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:5];
    [self.carousel autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:5];
    [self.carousel autoSetDimension:ALDimensionHeight toSize:400];
    
    
}

- (void) viewWillAppear:(BOOL)animated {
    
}

- (void) changeStyle
{
    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"Select Carousel Type" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    
    for (int i = 0; i < self.array.count;i++) {
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:self.array[i] style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
            [self actionTitle:action.title WithIndex: i];
        }];
        
        [alertView addAction:action];
    }
    
    [self presentViewController:alertView animated:YES completion:nil];

}

- (void) pressAction:(UIButton *) btn
{
    useButtons = !useButtons;
    [btn setTitle:useButtons?  @"Enable buttons" : @"Disable buttons" forState:UIControlStateNormal];
    [carousel reloadData];
}

- (void) actionTitle:(NSString *)title WithIndex:(NSInteger) index
{
    if (index >= 0)
    {
        //map button index to carousel type
        iCarouselType type = index;
        
        //carousel can smoothly animate between types
        [UIView beginAnimations:nil context:nil];
        self.carousel.type = type;
        [UIView commitAnimations];
        
        //update title
        self.navigationItem.title = title;
    }
    
}

- (NSInteger) numberOfItemsInCarousel:(iCarousel *)carousel
{
    return 200;
}


-(UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    if (useButtons)
    {
        //create a numbered button
        UIButton *newbutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        newbutton.frame = CGRectMake(0, 0, 200.0f, 200.0f);
        [newbutton setTitle:[NSString stringWithFormat:@"%li", (long)index] forState:UIControlStateNormal];
        newbutton.backgroundColor = [UIColor magentaColor];
        [newbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        newbutton.titleLabel.font = [newbutton.titleLabel.font fontWithSize:50];
        [newbutton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
        return newbutton;
    }
    else
    {
        //create a numbered view
        view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)];
        view.backgroundColor = [UIColor lightGrayColor];
        UILabel *label = [[UILabel alloc] initWithFrame:view.bounds];
        label.text = [NSString stringWithFormat:@"%li", (long)index];
        label.backgroundColor = [UIColor clearColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [label.font fontWithSize:50];
        [view addSubview:label];
        return view;
    }
}

- (CATransform3D)carousel:(__unused iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform
{
    //implement 'flip3D' style carousel:实现“flip3D”风格的旋转木马
    transform = CATransform3DRotate(transform, M_PI / 8.0f, 0.0f, 1.0f, 0.0f);
    return CATransform3DTranslate(transform, 0.0f, 0.0f, offset * self.carousel.itemWidth);
}


- (CGFloat)carousel:(__unused iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
    //customize carousel display
    switch (option)
    {
        case iCarouselOptionWrap:
        {
            //normally you would hard-code this to YES or NO
            return self.wrap;
        }
        case iCarouselOptionSpacing:
        {
            //add a bit of spacing between the item views:增加一些项目视图之间的间距
            return value * 1.05f;
        }
        case iCarouselOptionFadeMax:
        {
            if (self.carousel.type == iCarouselTypeCustom)
            {
                //set opacity based on distance from camera:基于距离相机设置不透明度
                return 0.0f;
            }
            return value;
        }
        case iCarouselOptionRadius:
        case iCarouselOptionShowBackfaces:
        case iCarouselOptionAngle:
        case iCarouselOptionArc:
        case iCarouselOptionTilt:
        case iCarouselOptionCount:
        case iCarouselOptionFadeMin:
        case iCarouselOptionFadeMinAlpha:
        case iCarouselOptionFadeRange:
        case iCarouselOptionOffsetMultiplier:
        case iCarouselOptionVisibleItems:
        {
            return value;
        }
    }
}

- (void)buttonTapped:(UIButton *)sender
{
    [[[UIAlertView alloc] initWithTitle:@"Button Tapped"
                                message:[NSString stringWithFormat:@"You tapped button number %li", (long)[carousel indexOfItemView:sender]]
                               delegate:nil
                      cancelButtonTitle:@"OK"
                      otherButtonTitles:nil] show];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
